package com.zheng.damo1.电商系统;

import java.util.ArrayList;
import java.util.Scanner;

public class LoginImpl implements LoginInterface{
    public static final ArrayList<User> LIST=new ArrayList<>();
    public static final Scanner INPUT=new Scanner(System.in);
    public static final User USER=new User();
    public static final CommodityInterFaceImpe CIFI=new CommodityInterFaceImpe();
    static {
        LIST.add(new User("张三","1234567a","zhangshan",23));
        LIST.add(new User("李四","1234567b","lisi",24));
        LIST.add(new User("王五","1234567c","wnagwu",25));
    }

    @Override
    public void LoginShow() {

        out:
        while (true){
            System.out.println("**************电器商城***************");
            System.out.println("             1.用户登录");
            System.out.println("             2.用户注册");
            System.out.println("             3.退出系统");
            System.out.println("**************电器商城***************");
            System.out.println("请输入您的操作选项");
            int count =new Scanner(System.in).nextInt();
            switch (count){
                case 1:
                    //用户登录
                    if (login()){
                        System.out.println("商品页面");
                        CIFI.CommodityShow();
                    }
                    break;
                case 2:
                    //用户注册
                    register();
                    break;
                case 3:
                    //退出系统
                    break out;
                default:
                    System.out.println("输入错误！！！");
                    break;
            }
        }
    }
    //用户登录
    @Override
    public boolean login() {
        System.out.println("请输入用户名>");
        String name=INPUT.nextLine();
        System.out.println("请输入密码");
        String passWord=INPUT.nextLine();
        for (User user : LIST) {
            if (user.getUserName().equals(name)) {
                if (user.getPassWord().equals(passWord)) {
                    System.out.println("登录成功  你好" +user.getOtherName());
                    USER.setUserName(user.getUserName());
                    USER.setPassWord(user.getPassWord());
                    USER.setOtherName(user.getOtherName());
                    USER.setAge(user.getAge());
                    return true;
                } else {
                    System.out.println("密码错误");
                    return false;
                }

            }
        }
        System.out.println("用户名错误");
        return false;
    }
    //用户注册
    @Override
    public void register() {
        String name="";
        String passWord="";
        Integer age=0;
        while (true){
            boolean namein=true;
            System.out.println("请输入用户名>");
            name=INPUT.nextLine();
            for (User user : LIST) {
                if (user.getUserName().equals(name)) {
                    System.out.println("用户名存在请重新输入");
                    namein = false;
                    break;
                }
            }
            if (namein){
                break;
            }
        }//判断用户名是否存在
        while (true){
            System.out.println("请输入密码");
            passWord=INPUT.nextLine();
            System.out.println("请再次输入密码");
            String word=INPUT.nextLine();
            if (passWord.length()<6){
                System.out.println("密码长度不能小于6");
            }else if (passWord.equals(word)){
                break;
            }else {
                System.out.println("两次密码不一样");
            }
        }//判断密码是否大于6
        while (true){
            System.out.println("请输入你的年龄");
            age=Integer.valueOf(INPUT.nextLine());
            if (age>0&&age<100){
                break;
            }else {
                System.out.println("年龄必须在1-100之间");
            }
        }//判断年龄
        System.out.println("请输入昵称");
        String otherName=INPUT.nextLine();
        LIST.add(new User(name,passWord,otherName,age));
        System.out.println("注册成功");
    }


}
